﻿
using SBSimpleLib.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SBSimpleLib
{
    public abstract class KTState : MyWoW.FiniteStateMachine.State
    {
        protected String TAG = "KTS";

        internal static int MaxPriorityValue = 9999;
        internal static int MinPriorityValue = 0;
        private bool isRunning = false;

        public bool IsRunning {
            get
            {
                return isRunning;
            }
            set
            {
                isRunning = value;
                Logger.debug(TAG, "running: " + isRunning);
            }
        }
        protected bool initialized = false;
        private bool isMovementState = false;

        public String whyNotRun = "Not Explained.";

        public override String DisplayName { 
            get {
                return "KTState";
            }
        }

        public override bool NeedToRun
        {
            get
            {
                bool ret = doNeedToRun();

                if (!ret)
                {
                    IsRunning = false;
                    if (whyNotRun != "")
                    {
                        Logger.debug(TAG + "-" + DisplayName, "No Run: " + whyNotRun);
                    }
                }
                else
                {
                    // DEBUG Logger.debug(TAG, "NeedToRun: " + ret + ", " + ToString());
                }

                
                return ret;

            }
        }

        public override int Priority
        {
            get
            {
                return MaxPriorityValue;
            }
        }

        public override void Run()
        {

            IsRunning = true;
            if (!initialized)
            {
                Logger.debug(TAG, "Run First Time");
                doRunFirstTime();
                initialized = true;
            }
            else
            {
                Logger.debug(TAG, "Run");
                doRun();
                }


        }

        public override String ToString()
        {
            return DisplayName + ": runing: " + isRunning + ", init: " + initialized + ", isMoveState: " + isMovementState;
        }

        public abstract void doRun();
        public virtual void doRunFirstTime()
        {
            // By Default we execute doRun() on first time
            doRun();
        }

        public abstract bool doNeedToRun();
        
    }
}
